package fr.unice.polytech.plim.podometre;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;

public class Start extends Activity {

	private boolean startStop = true;
	//private TextView tv;
	private Intent myIntent;
	private LocationManager locationManager;
	public SQLiteDatabase bdd ;
	public Bdd mabase = new Bdd(this, Bdd.DATABASE_NAME, null, Bdd.DATABASE_VERSION);
	ContentValues nouvelleValeurs = new ContentValues();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_start);
		final ToggleButton start = (ToggleButton) findViewById(R.id.button1);
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.current_position);
		String context = Context.LOCATION_SERVICE;
		locationManager = (LocationManager) getSystemService(context);
		
		setBdd(mabase.getWritableDatabase());
		bdd.delete(Bdd.DATABASE_TABLE,"1",null);
		
		Criteria criteria = new Criteria();
	    criteria.setAccuracy(Criteria.ACCURACY_FINE);
	    criteria.setAltitudeRequired(false);
	    criteria.setBearingRequired(false);
	    criteria.setCostAllowed(true);
	    criteria.setPowerRequirement(Criteria.POWER_LOW);
	    String provider = locationManager.getBestProvider(criteria, true);
		Location location = locationManager.getLastKnownLocation(provider);
		updateWithNewLocation(location);
		locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);
		start.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				if(startStop == true){
					myIntent = new Intent(view.getContext(), ServiceBDD.class);
					startService(myIntent);
					startStop = false;
				}
				else {
					//stopService(myIntent);
					myIntent = new Intent(view.getContext(), MyMap.class);
					stopService(new Intent(view.getContext(), ServiceBDD.class));
					startActivityForResult(myIntent, 0);
					startStop = true;
				}
			}
		});
	}
private final LocationListener locationListener = new LocationListener() {
		
		
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onProviderDisabled(String provider) {
			updateWithNewLocation(null);
			
		}
		
		@Override
		public void onLocationChanged(Location location) {
			updateWithNewLocation(location);
			
		}
	};
	private void updateWithNewLocation(Location location) {
		//tv = (TextView) findViewById(R.id.tv);
		//String latLongSpeedString = "Aucun emplacement trouve";
		if (location != null){
			double lat = location.getLatitude();
			double llg = location.getLongitude();
			double alt = location.getAltitude();
			double speed = location.getSpeed();
			long time = location.getTime();
			
			nouvelleValeurs.put(Bdd.KEY_DATE, time);
			nouvelleValeurs.put(Bdd.KEY_POSITION_ALT,alt );
			nouvelleValeurs.put(Bdd.KEY_POSITION_LAT, lat);
			nouvelleValeurs.put(Bdd.KEY_POSITION_LONG, llg);
			nouvelleValeurs.put(Bdd.KEY_VITESSE, speed);
			bdd.insert(Bdd.DATABASE_TABLE, null, nouvelleValeurs);
			bdd.close();
			//latLongSpeedString = "Lattitude: " + lat + "\nLongitude: " + llg + "\nAlt: " + alt+"\nVitesse: " + speed;
		}
		//tv.setText(tv.getText()+"Votre position est:\n" + latLongSpeedString);
		//tv.setText(tv.getText()+"Bdd:\n");
		/*String[] result_columns = new String[]{
			Bdd.KEY_ID, Bdd.KEY_DATE, Bdd.KEY_POSITION_LAT, Bdd.KEY_POSITION_LONG, Bdd.KEY_POSITION_ALT,Bdd.KEY_VITESSE };
			*/
		//String where =  Bdd.KEY_ID + "= 130 "; 
		//Cursor cursor = bdd.query(Bdd.DATABASE_TABLE, null, null, null, null, null, null);
		//cursor.moveToFirst();
		//tv.setText(tv.getText()+" \n enregistrement : "+""+cursor.getCount()+"\n");
		
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_start, menu);
		return true;
	}
	public SQLiteDatabase getBdd() {
		return bdd;
	}


	public void setBdd(SQLiteDatabase bdd) {
		this.bdd = bdd;
	}


}
